package com.zbkj.common.utils;

import okhttp3.*;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.json.JSONObject;

public class BaiDu {
    public static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().readTimeout(300, TimeUnit.SECONDS).build();
    public static final String APP_ID = "6937089";
    public static final String API_KEY = "lbDJ5fYjsyl1FDZ8RgMKP6OS";
    public static final String API_SECRET = "xUBIIOOWT0sDEAaz6wKuyKOQZMMwmkQ4";
  /*  public static void main(String []args) throws IOException {
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "");
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/oauth/2.0/token?client_id="+API_KEY+"&client_secret="+API_SECRET+"&grant_type=client_credentials")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept", "application/json")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        System.out.println(response.body().string());

    }*/

    public static void main(String[] args) {
        try {
            callOCRAPI("http://182.92.162.207:8887/ai-file/1750126579393.jpg");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String callOCRAPI(String url) throws Exception {
        // 使用FormBody.Builder构建参数
        FormBody formBody = new FormBody.Builder()
                .add("location", "false")
                .add("probability", "false")
                .add("medi_query", "100000")
                .add("url", url)
                .build();

        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/rest/2.0/ocr/v1/medical_invoice?access_token=" + getAccessToken())
                .post(formBody)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Accept", "application/json")
                .build();

        try (Response response = HTTP_CLIENT.newCall(request).execute()) {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
            return response.body().string();
        }

    }

    /**
     * 从用户的AK，SK生成鉴权签名（Access Token）
     *
     * @return 鉴权签名（Access Token）
     * @throws IOException IO异常
     */
    static String getAccessToken() throws Exception {
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType,
                "grant_type=client_credentials&client_id=" + API_KEY +
                        "&client_secret=" + API_SECRET);

        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/oauth/2.0/token")
                .post(body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();

        try (Response response = HTTP_CLIENT.newCall(request).execute()) {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
            String responseBody = response.body().string();
            return new JSONObject(responseBody).getString("access_token");
        }
    }
}
